package de.jmda.sample.xml;

import static de.jmda.core.util.FileUtil.toSourceFilePath;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

import de.jmda.sample.xml.p1.XMLType1;
import de.jmda.sample.xml.p2.XMLType2;

public class JUTJAXBIndexDataBuilder
{
	private final static File SOURCE_ROOT = new File("./src/test/java");

	@Test
	public void testBuild()
	{
		Map<File, Set<String>> map = JAXBIndexDataBuilder.build(SOURCE_ROOT);

		boolean foundDirectoryForXMLType1 = false;
		boolean foundDirectoryForXMLType2 = false;
		boolean foundFilenameForXMLType1 = false;
		boolean foundFilenameForXMLType2 = false;

		String sourceFileNameForXMLType1 =
				  SOURCE_ROOT.getAbsolutePath()
				+ File.separator + toSourceFilePath(XMLType1.class);
		String sourceFileNameForXMLType2 =
				  SOURCE_ROOT.getAbsolutePath()
				+ File.separator + toSourceFilePath(XMLType2.class);

		for (File packageDirectory : map.keySet())
		{
			if (sourceFileNameForXMLType1.startsWith(packageDirectory.getAbsolutePath()))
			{
				foundDirectoryForXMLType1 = true;

				for (String typeName : map.get(packageDirectory))
				{
					if (typeName.equals(XMLType1.class.getSimpleName()))
					{
						foundFilenameForXMLType1 = true;

						File file = new File(packageDirectory, typeName + ".java");
						assertTrue("file " + file + " does not exist", file.exists());

						break;
					}
				}
			}
			else if (sourceFileNameForXMLType2.startsWith(packageDirectory.getAbsolutePath()))
			{
				foundDirectoryForXMLType2 = true;

				for (String typeName : map.get(packageDirectory))
				{
					if (typeName.equals(XMLType2.class.getSimpleName()))
					{
						foundFilenameForXMLType2 = true;

						File file = new File(packageDirectory, typeName + ".java");
						assertTrue("file " + file + " does not exist", file.exists());

						break;
					}
				}
			}
		}

		assertTrue(
				"package directory for " + XMLType1.class.getName() + " not found",
				foundDirectoryForXMLType1);
		assertTrue(
				"package directory for " + XMLType2.class.getName() + " not found",
				foundDirectoryForXMLType2);
		assertTrue(
				"filename for " + XMLType1.class.getName() + " not found",
				foundFilenameForXMLType1);
		assertTrue(
				"filename for " + XMLType2.class.getName() + " not found",
				foundFilenameForXMLType2);

		System.out.println(asString(map));
	}

	@Test
	public void testGetPackageList()
	{
		JAXBIndexDataBuilder builder =
				new JAXBIndexDataBuilder(
						XMLRootElementProcessor.process(SOURCE_ROOT));
		
		System.out.println(builder.getPackageList());
	}

	@Test
	public void testGetIndexFileData()
	{
		JAXBIndexDataBuilder builder =
				new JAXBIndexDataBuilder(
						XMLRootElementProcessor.process(SOURCE_ROOT));

		System.out.println(asString(builder.getIndexFileData()));
	}

  private String asString(Map<File, Set<String>> map)
	{
		StringBuffer result = new StringBuffer();
		
		for (File file : map.keySet())
		{
			result.append("package directory: " + file.getAbsolutePath() + "\n");
			
			for (String filename : map.get(file))
			{
				result.append("\tfilename: " + filename + "\n");
			}
		}
		
		return result.toString();
	}
}